package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/admin/product")
public class SkuManagerController {

    //自动注入
    @Autowired
    private ManagerService managerService;

    /**
     商品上架
     * admin/product/onSale/{skuId}

    */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){

        //调用Service层的方法实现上架功能
        managerService.onSale(skuId);

        return Result.ok();
    }



    /**
     商品下架
     * admin/product/cancelSale/{skuId}

    */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){

        //调用Service层的方法实现 下架功能
        managerService.cancelSale(skuId);

        return Result.ok();
    }



    /**
     分页查询skuInfo
     * admin/product/list/{page}/{limit}

    */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuListByPage(@PathVariable Long page,
                       @PathVariable Long limit){

        //封装分页条件为对象
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        //调用Service层方法 实现分页查询skuInfo的功能
        IPage<SkuInfo> skuInfoIPage = managerService.getSkuListByPage(skuInfoPage);

        return Result.ok(skuInfoIPage);
    }



    /**
     admin/product/saveSkuInfo
     * 保存skuInfo

    */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        managerService.saveSkuInfo(skuInfo);

        return Result.ok();
    }



    /**
     /admin/product/spuSaleAttrList/{spuId}
     * 根据spuId 查询销售属性

    */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId) {

        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);

        return Result.ok(spuSaleAttrList);

    }
        /**
         admin/product/spuImageList/{spuId}
         * 根据spuId 获取spuImage 集合

         */
        @GetMapping("/spuImageList/{spuId}")
        public Result spuImageList(@PathVariable Long spuId){
            List<SpuImage> spuImageList=managerService.spuImageList(spuId);

            return Result.ok(spuImageList);
        }


    }